package com.colde.salary.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;

import org.apache.poi.ss.usermodel.Workbook;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

public class PoiUtil {
	
	/**
	 * 
	 * @param title	表标题
	 * @param sheetName 表名
	 * @param filePath	文件导出路径
	 * @param fileName		文件名称（不带.xls后缀）
	 * @param pojoClass	导出实体
	 * @param dataSet		导出信息列表
	 * @return
	 */
	public static boolean exportExcel(String title, String sheetName,String filePath,String fileName,Class<?> pojoClass,
            Collection<?> dataSet) {
		if(null!=dataSet&&!dataSet.isEmpty()) {
			File savefile = new File(filePath);
	        if (!savefile.exists()) {
	            savefile.mkdirs();
	        }
	        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName),pojoClass,dataSet);
	        FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(filePath+fileName+".xls");
				workbook.write(fos);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}finally {
				if(null!=fos) {
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(null!=workbook) {
					try {
						workbook.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return true;
		}
		return false;
	}
}
